package com.schoolegard.services.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.schoolegard.services.UsersService;
import com.schoolgard.dao.RoleDao;
import com.schoolgard.dao.RolePrivilegeDao;
import com.schoolgard.dao.RoleToUsersDao;
import com.schoolgard.dao.UsersDao;
import com.schoolgard.entitybean.Role;
import com.schoolgard.entitybean.RoleOfUsers;
import com.schoolgard.entitybean.Users;

public class UsersServiceImpl implements UsersService {

	//系统用户
	private UsersDao usersDao;
	private RoleToUsersDao roleToUsersDao;
	private RoleDao roleDao;
	private RolePrivilegeDao rolePrivilegeDao;
	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}
	public void setRoleToUsersDao(RoleToUsersDao roleToUsersDao) {
		this.roleToUsersDao = roleToUsersDao;
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	public void setRolePrivilegeDao(RolePrivilegeDao rolePrivilegeDao) {
		this.rolePrivilegeDao = rolePrivilegeDao;
	}


	@Override
	public Users findByLoginId(String loginId) {
		DetachedCriteria critiria = DetachedCriteria.forClass(Users.class);
		critiria.add(Restrictions.eq("loginid", loginId));
		List<Users> users = usersDao.findByCriteria(critiria);
		return (users != null && users.size()>0)?users.get(0):null;
	}
	@Override
	public List<RoleOfUsers> getRoleToUser(Users user) {
		DetachedCriteria critiria = DetachedCriteria.forClass(RoleOfUsers.class);
		return null;
	}
	@Override
	public List<Users> findAllUser() {
		return usersDao.findAll();
	}
	@Override
	public Users findById(Long id) {
		return usersDao.findById(id);
	}
	@Override
	public void saveOrUpdate(Users user) {
		usersDao.createOrModify(user);
	}
	
	@Override
	public void delete(Users user) {
		usersDao.remove(user);
	}
	
	@Override
	public void saveAuthority(Long userId, String roles) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RoleOfUsers.class);
		criteria.add(Restrictions.eq("users.id", userId));
		List<RoleOfUsers> roleOfUsers = roleToUsersDao.findByCriteria(criteria);
		RoleOfUsers roleOfUser = null;
		if(roleOfUsers == null || roleOfUsers.size() <= 0){ //新增关系
		    roleOfUser = new RoleOfUsers();
		}else{
			roleOfUser = roleOfUsers.get(0);
		}
		roleOfUser.setEnable("1");
		roleOfUser.setRole(roles);
		roleOfUser.setUsers(findById(userId));
		roleToUsersDao.createOrModify(roleOfUser);
	}
	@Override
	public Role findRoleById(Long roleId) {
		return roleDao.findById(roleId);
	}
	@Override
	public String findRolesByUserId(Long userId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RoleOfUsers.class);
		criteria.add(Restrictions.eq("users.id", userId));
		List<RoleOfUsers> roleOfUsers = roleToUsersDao.findByCriteria(criteria);
		if(roleOfUsers != null && roleOfUsers.size() > 0){
			RoleOfUsers roleosuser = roleOfUsers.get(0); //一个用户只会对应一条数据
			return roleosuser.getRole();
		}
		return null;
	}
	
	
	public Users findByRoles(String[] acceptors) {
		
		return null;
	}
	@Override
	public Role findRoleByRoleName(String roleName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		criteria.add(Restrictions.eq("name", roleName));
		List<Role> roles = roleDao.findByCriteria(criteria);
		if(roles != null && roles.size() > 0){
			Role role = roles.get(0);
			return role;
		}
		return null;
	}
	
	
}
